﻿// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using Microsoft.AspNetCore.Components;
using SupperTreeSample.Shared.Data;

namespace SupperTreeSample.Shared.Components;

public partial class FoodMenus
{
    private ElementReference FoodMenuElement { get; set; }

    [Inject]
    [NotNull]
    private DialogService? DialogService { get; set; }

    [Inject]
    [NotNull]
    private SwalService? SwalService { get; set; }

    [Parameter]
    [NotNull]
    public List<Food>? Items { get; set; }

    private Food? CurrentFood { get; set; }

    private int IndexOf(Food item) => Items.IndexOf(item);

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    protected override ElementReference GetElement() => FoodMenuElement;

    private Task Update(Food food) => DialogService.ShowSaveDialog<EditFood>("菜品编辑", () =>
    {
        if (CurrentFood != null)
        {
            var index = Items.IndexOf(food);
            Items.Remove(food);
            Items.Insert(index, CurrentFood);
            StateHasChanged();
        }
        return Task.FromResult(true);
    }, new Dictionary<string, object?>()
    {
        [nameof(EditFood.Value)] = food,
        [nameof(EditFood.OnValueChanged)] = new Func<Food, Task>(food =>
        {
            CurrentFood = food;
            return Task.CompletedTask;
        })
    });

    private Task Add(Food? food = null) => DialogService.ShowSaveDialog<EditFood>("增加菜品", () =>
    {
        var index = food != null ? Items.IndexOf(food) + 1 : 0;
        if (CurrentFood != null)
        {
            Items.Insert(index, CurrentFood);
            StateHasChanged();
        }
        return Task.FromResult(true);
    }, new Dictionary<string, object?>()
    {
        [nameof(EditFood.OnValueChanged)] = new Func<Food, Task>(food =>
        {
            CurrentFood = food;
            return Task.CompletedTask;
        })
    });

    private async Task Delete(Food food)
    {
        var result = await SwalService.ShowModal(new SwalOption()
        {
            Title = $"您确定要删除 {food.Name} 菜品吗？",
            Category = SwalCategory.Warning
        });
        if (result)
        {
            // 删除数据源
            Items.Remove(food);
        }
    }

    private void MovePrev(Food food)
    {
        var index = Items.IndexOf(food);
        if (index > 0)
        {
            Items.Remove(food);
            Items.Insert(index - 1, food);
        }
    }

    private void MoveNext(Food food)
    {
        var index = Items.IndexOf(food);
        if (index < Items.Count - 1)
        {
            Items.Remove(food);
            Items.Insert(index + 1, food);
        }
    }

    private void MoveFirst(Food food)
    {
        Items.Remove(food);
        Items.Insert(0, food);
    }

    private void MoveLast(Food food)
    {
        Items.Remove(food);
        Items.Add(food);
    }
}
